package com.huike.controller;

import com.huike.po.User;
import com.huike.po.p2pMessage;
import com.huike.pojo.FileMsg;
import com.huike.pojo.LoginForm;
import com.huike.pojo.Response;
import com.huike.service.P2pService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;

@Controller
/**
 * 个人通信相关的请求处理
 */
@RequestMapping("/p2p")
public class P2pController {
    @Autowired
    private P2pService p2pService;
    @Autowired
    public Response response;

    @RequestMapping(value = "/chatRecord" )
    @ResponseBody
    public Response getChatRecord(String accountFirst, String accountSecond, HttpSession session) {
        if (null != session.getAttribute("loginUser")) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            List<p2pMessage> p2pMessageList = p2pService.getChatRecord(accountFirst,accountSecond);
            response.setCode("200");
            response.setContent(p2pMessageList);
        }
        return response;
    }

    @RequestMapping(value = "/sendMessage" )
    @ResponseBody
    public Response sendMessage(@RequestBody p2pMessage message, HttpSession session) {
        if (p2pService.sendMessage(message)) 
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }


    @RequestMapping(value = "/deleteOfflineMsg" )
    @ResponseBody
    public Response deleteOfflineMsg(String userAccount, String friendAccount, HttpSession session) {
        if (p2pService.deleteOfflineMsg(friendAccount, userAccount))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }

    @RequestMapping(value = "/roamingRecord")
    @ResponseBody
    public Response getRoamingRecord(String accountFirst,String accountSecond, HttpSession session) {
        if(null != session.getAttribute("loginUser")) {
            response.setCode("400");
            response.setContent("请先登录");
        }
        else {
            List<p2pMessage> p2pMessageList = p2pService.getRoamingRecord(accountFirst,accountSecond);
            response.setContent("200");
            response.setContent(p2pMessageList);
        }
        return response;
    }

    @RequestMapping(value = "/sendFileMessage" )
    @ResponseBody
    /**
     * 发送文件消息
     */
    public Response sendFile(@RequestParam("fileContent")MultipartFile fileContent,
                             @RequestParam("msgFrom") String msgFrom,
                             @RequestParam("msgTo") String msgTo,
                             @RequestParam("msgTime") String msgTime,
                             @RequestParam("msgClient") String msgClient){
        FileMsg message = new FileMsg();
        message.setMsgFrom(msgFrom);
        message.setMsgTo(msgTo);
        message.setMsgContent(fileContent.getOriginalFilename());
        message.setMsgTime(msgTime);
        message.setMsgClient(msgClient);
        if (p2pService.sendFile(message, fileContent))
            response.setCode("200");
        else
            response.setCode("500");
        response.setContent("");
        return response;
    }
    @RequestMapping(value = "/getFileList" )
    @ResponseBody
    /**
     * 获取文件记录
     */
    public Response getFileList(@RequestParam("msgFrom") String msgFrom, @RequestParam("msgTo") String msgTo){
        response.setCode("200");
        response.setContent(p2pService.getFileList(msgFrom, msgTo));
        return response;
    }
    @RequestMapping(value = "/downLoadFile" )
    @ResponseBody
    /**
     * 文件下载，返回文件二进制流，前端进行处理下载
     */
    public Response downLoadFile(@RequestParam("msgFrom") String msgFrom, @RequestParam("msgTo") String msgTo, @RequestParam("fileName") String fileName){
        FileMsg file = p2pService.getFile(msgFrom, msgTo, fileName);
        if(file == null) {
            response.setCode("404");
            response.setContent("");
        } else {
            response.setCode("200");
            response.setContent(file);
        }

        return response;
    }


}
